package org.example.controller;

import org.example.annotation.ClearAndReloadCacheDict;
import org.example.constant.KeyConstant;
import org.example.domain.DictData;
import org.example.domain.Plant;
import org.example.domain.Type;
import org.example.dto.DictDataQueryDTO;
import org.example.dto.DictQueryDTO;
import org.example.result.PageResult;
import org.example.result.Result;
import org.example.service.DictDataService;
import org.example.service.DictService;
import org.example.service.PlantService;
import org.example.service.TypeService;
import org.example.vo.DictVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject：blockchain-APTrace
 * @CreateTime 2024-05-28-8:54
 * @Author SweetPig
 */
@RequestMapping("dict")
@RestController
public class DictController {

    @Autowired
    private DictService dictService;
    @Autowired
    private DictDataService dictDataService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private PlantService plantService;
    @Autowired
    private TypeService typeService;


    @RequestMapping("plantList")
    public Result plantList() {
        List<Plant> list = plantService.plantList();
        return Result.success(list);
    }

    @RequestMapping("typeList")
    public Result typeList() {
        List<Type> list = typeService.typeList();
        return Result.success(list);
    }

    @RequestMapping("dictList")
    public Result dictList(DictQueryDTO dictQueryDTO) {
        String key = KeyConstant.REDIS_DICT_KEY + dictQueryDTO.getPageNum();
        if (!redisTemplate.hasKey(key)) {
            PageResult pageResult = dictService.list(dictQueryDTO);
            if (pageResult.getTotal() != 0) {
                redisTemplate.opsForValue().set(key, pageResult);
            }
            return Result.success(pageResult);
        }
        Object pageResult = redisTemplate.opsForValue().get(key);
        return Result.success(pageResult);

    }

    @RequestMapping("addDict")
    public Result add(DictQueryDTO dictQueryDTO) {
        boolean i = dictService.save(dictQueryDTO);
        return Result.success(i);
    }

    @RequestMapping("updateDict")
    @ClearAndReloadCacheDict()
    public Result updateDict(DictQueryDTO dictQueryDTO) {
        boolean dict = dictService.update(dictQueryDTO);
        redisTemplate.delete(KeyConstant.REDIS_DICT_KEY + dictQueryDTO.getPageNum());
        return Result.success(dict);
    }

    @RequestMapping("delById")
    public Result delById(Integer id) {
        boolean i = dictService.removeById(id);
        return Result.success(i);
    }

    @RequestMapping("getDictData")
    public Result getDictData(DictDataQueryDTO dictDataQueryDTO) {
        String key = KeyConstant.REDIS_DICT_DATA_KEY + dictDataQueryDTO.getDictId();
        if (!redisTemplate.hasKey(key)) {
            List<DictData> dictDataList = dictDataService.getById(dictDataQueryDTO.getDictId());
            System.out.println(dictDataList);
            redisTemplate.opsForList().rightPushAll(key, dictDataList);
            //设置过期时间
            redisTemplate.expire(key, 2, TimeUnit.HOURS);
            return Result.success(dictDataList);
        }
        List list = redisTemplate.opsForList().range(key, 0, -1);
        return Result.success(list);
    }


}
